<?php include('../includes/header.php'); ?>
<?php include('../includes/mysqli_connect.php'); ?>
<?php include('../includes/function.php'); ?>
<?php include('../includes/slide-admin.php'); ?>
  
    <?php 
        if(isset($_GET['pid'])&& filter_var($_GET['pid'],FILTER_VALIDATE_INT,array('min_range'=>1))){
            $pid=$_GET['pid'];
        }
        else {
            redirect_to('admin/view_page.php');
        }       
        if($_SERVER['REQUEST_METHOD']=='POST')
        {
            $errors=array();
            if(empty($_POST['page_name'])){
                $errors[]='page_name';
            } else {
                 $page_name=mysqli_real_escape_string($dbc,strip_tags($_POST['page_name']));
            }
            
            if(isset($_POST['category']) && filter_var($_POST['category'],FILTER_VALIDATE_INT,array('min_range'=>1))){
            $cat_id=$_POST['category'];
            }
            else{
                $errors[]="category";            
            }  
            
            if(isset($_POST['position']) && filter_var($_POST['position'],FILTER_VALIDATE_INT,array('min_range'=>1))){
            $position=$_POST['position'];
            }
            else{
                $errors[]="position";            
            } 
            
            if(empty($_POST['content'])){
                $errors[]='content';
            } else {
                $content=mysqli_real_escape_string($dbc,$_POST['content']);
            }
            
            if(empty($errors)){
                //Neu khong co loi xay ra thi bat dau chen du lieu vao csdl 
                $q="UPDATE pages set page_name='{$page_name}',cat_id={$cat_id},position={$position},content='{$content}' where page_id={$pid}";
                $r=mysqli_query($dbc,$q);
                confirm_query($r,$q);
                if(mysqli_affected_rows($dbc)==1){
                    $messages="<p class='success'>The page was edited successfully</p>";
                } else {
                    $messages="<p class='warning'>The page could not be edit</p>";
                }
            } else {
                $messages ="<p class='warning'>Please fill all the fields</p>";
            }           
        }
    ?>
    
    <?php 
       $q1="Select page_name,content,position,cat_id from pages where page_id ={$pid}";
       $r1=mysqli_query($dbc,$q1);
       confirm_query($r1,$q1);
       if(mysqli_num_rows($r1)==1){
            list($page_name,$content,$position,$cat_id) = mysqli_fetch_array($r1,MYSQLI_NUM);            
       } else {
        $messages="<p class='warning'>The pages does not exist</p>";
       }                           
    ?>
    
     <div id="content">
     <h2>Edit <?php if(isset($page_name)) echo $page_name?></h2>
     <?php
        if(!empty($messages))echo $messages;
     ?>
      <form id="add_page" action="" method="post">
          <fieldset>
            <legend>Add a Page </legend>
            <div> 
                <label for="page">Page Name : <span class="required">*</span></label>
                    <?php if(isset($errors) && in_array('page_name',$errors))
                            {echo "<p class='warning'>Please fill the page name </p>";}
                    ?>                    
                <input type="text" name="page_name" id="page_name" value="<?php if(isset($page_name)) echo $page_name?>" size="20" maxlength="80" tabindex="1"/>                
            </div>
            <div>
                <label for="category"> All category : <span class="required">*</span></label>
                     <?php if(isset($errors) && in_array('category',$errors))
                            {echo "<p class='warning'>Please select the category  </p>";}
                    ?>
                <select name="category">
                  <?php
                  $q="Select cat_id,cat_name from categories order by position ASC ";
                  $r=mysqli_query($dbc,$q);
                  if(mysqli_num_rows($r)>0){
                    while($cats=mysqli_fetch_array($r,MYSQLI_NUM)){
                          echo "<option value='{$cats[0]}'";                            
                          if(isset($cat_id) && $cat_id==$cats[0]) echo "selected='selected'";                            
                          echo ">".$cats[1]."</option>";
                    }
                  }
                  ?>
                </select>
                    
            </div>
            <div>
                <label for="position"> Position : <span class="required">*</span></label>
                  <?php if(isset($errors) && in_array('position',$errors))
                            {echo "<p class='warning'>Please select the position  </p>";}
                    ?>
                <select name="position">
                 <?php
                    $q="SELECT count(page_id) AS count from pages";
                    $r=mysqli_query($dbc,$q) or die ("Query {$q} <br /> MySql Error  ".mysqli_error($dbc));;
                    if(mysqli_num_rows($r)==1) {
                        list($num)=mysqli_fetch_array($r,MYSQLI_NUM);
                        for ($i=1;$i <=$num+1;$i++)//tao vong for de ra option +1  gia tri cho position
                        {
                            echo "<option value='{$i}'";                            
                            if(isset($position) && $position==$i) echo "selected='selected'";                            
                            echo ">".$i."</option>";
                        }   
                    }
                  ?>
                </select>
            </div>
            <div >
               <label for="page-content">Page Content <span class="required">*</span></label>
                  <?php if(isset($errors) && in_array('content',$errors))
                            {echo "<p class='warning'>Please fill the content </p>";}
                    ?>
               <textarea name="content" cols="50" rows="20"><?php if(isset($content)) echo $content?></textarea>
            </div>
          </fieldset>
            
      
        <input type="submit" value="Add Page"/>
      </form>

    </div><!--end content-->
<?php include('../includes/slide-b.php'); ?>
<?php include('../includes/footer.php'); ?>
    
    